package com.xiaoyu.merchant.ui.activity.commodity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.techfansy.moreImageSelected.ImagePicker;
import com.techfansy.moreImageSelected.MoreImageMethod;
import com.techfansy.moreImageSelected.bean.ImageItem;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.loader.StoreCreBanModel;
import com.xiaoyu.merchant.loader.StoreImageLoader;
import com.xiaoyu.merchant.model.GoodsCateGoryBean;
import com.xiaoyu.merchant.ui.dialog.AddSpecificationDialog;
import com.xiaoyu.merchant.ui.dialog.ChooseCateGoryDialog;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//上传商品
public class UploadActivity extends BaseActivityWithTitle {
    private TextView mRightTxt;
    private final int IMAGE_PICKER = 100;
    @BindView(R.id.fragment_upload_detail_banner)
    Banner mBanner;//轮播图
    @BindView(R.id.fragment_upload_banner_img)
    ImageView mBannerImg;
    @BindView(R.id.fragment_upload_choose_classify_txt)
    TextView classifyTxt;//选择的商品类别
    @BindView(R.id.goods_show_img_layout)
    LinearLayout showImgLayout;//商品展示图片布局
    @BindView(R.id.goods_add_show_img)
    ImageView addImg;//商品展示图片图片添加按钮

    private View mView;
    private MoreImageMethod moreImageMethod;
    private ImagePicker imagePicker;
    private List<ImageItem> mList = new ArrayList<>();//选中的图片list
    private List<StoreCreBanModel> bannerImgs = new ArrayList<>();//轮播图数据列表
    private List<ImageItem> mGoodsImgList = new ArrayList<>();//商品展示图列表

    private GoodsCateGoryBean.GoodsCateGoryParam mSelectCatagory = null;//选中的商品类别
    private ChooseCateGoryDialog mCateGoryDialog;//选择商品列表弹窗

    private boolean isBanner = true;//true:轮播图  false:商品展示图
    private String goodstagid;

    private boolean isClick = true;//true:点击跳转预览 false:发布商品

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("作品上传");
        LayoutInflater.from(this).inflate(R.layout.activity_upload, parent, true);
        ButterKnife.bind(this);
        initBanner();
        moreImageMethod = new MoreImageMethod();
        imagePicker = ImagePicker.getInstance();
        imagePicker.clear();
        initView();
    }

    private void initView() {
        mRightTxt = new TextView(this);
        mRightTxt.setOnClickListener(this);
        mRightTxt.setText("预览");
        mRightTxt.setTextColor(Color.parseColor("#ff0000"));
        mRightTxt.setTextSize(16);
        addRightView(mRightTxt, 0);
    }

    /**
     * 选择分类
     */
    @OnClick(R.id.fragment_upload_choose_classify)
    public void chooseCategoryClick() {
        if (mCateGoryDialog == null) {
            mCateGoryDialog = new ChooseCateGoryDialog(this);
        }
        mCateGoryDialog.setOnSelectCategoryListener(onSelectCategoryListener);
        mCateGoryDialog.show(mSelectCatagory);
    }

    /**
     * 商品类别选择监听
     */
    private ChooseCateGoryDialog.OnSelectCategoryListener onSelectCategoryListener = new ChooseCateGoryDialog.OnSelectCategoryListener() {
        @Override
        public void onSelected(GoodsCateGoryBean.GoodsCateGoryParam category) {
            if (category != null) {
                mSelectCatagory = category;
                classifyTxt.setText(mSelectCatagory.getGoodstagname());
                classifyTxt.setTextColor(Color.BLACK);
                goodstagid = mSelectCatagory.getGoodstagid();
            }
        }
    };
    /**
     * 选择规格
     */
    @OnClick(R.id.fragment_upload_choose_specifications)
    public void chooseSpecificationClick() {
        AddSpecificationDialog dialog = new AddSpecificationDialog(this, true, true);
        dialog.show();
    }

    //初始化轮播图
    private void initBanner() {
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置是否自动轮播（不设置则默认自动）
        mBanner.isAutoPlay(true);
        //设置是否允许手动滑动轮播图
        mBanner.setViewPagerIsScroll(true);
        //设置轮播图片间隔时间（不设置默认为2000）
        mBanner.setDelayTime(2000);
        mBanner.setImageLoader(new StoreImageLoader());
        mBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (bannerImgs.get(position).getType() == 1) {
                    moreImageMethod.setClick(UploadActivity.this, mList, imagePicker);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mRightTxt) {
            if (isClick == true) {
                //TODO:跳转到预览页面
                startActivity(new Intent(this, PreviewActivity.class));
                mRightTxt.setText("发布");
            } else {
                ToastUtil.showToast(this, "发布成功");
            }
        }
    }
}
